package com.qiaofang.applet.rest;

import com.qiaofang.applet.common.context.QfContextHolder;
import com.qiaofang.applet.common.dto.DataRequest;
import com.qiaofang.applet.common.dto.DataResponse;
import com.qiaofang.applet.common.dto.PageDataRequest;
import com.qiaofang.applet.common.exception.BusinessException;
import com.qiaofang.applet.common.util.PageUtil;
import com.qiaofang.applet.service.other.domain.Assets;
import com.qiaofang.applet.service.other.service.AssetsService;
import com.qiaofang.applet.service.other.service.dto.AssetsDetailDto;
import com.qiaofang.applet.service.other.service.dto.AssetsQueryDto;
import com.qiaofang.applet.service.other.service.dto.AssetsTypeDto;
import com.qiaofang.applet.service.other.service.mapstruct.AssetsMapper;
import com.qiaofang.applet.service.property.service.dto.IdDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * @author daiyuxaing
 */
@Slf4j
@RestController
@RequestMapping("/api/pcweb/assets")
@Api(tags = "资讯：资讯服务")
public class AssetsController {

    @Autowired
    private AssetsService assetsService;

    @Autowired
    private AssetsMapper assetsMapper;

    @PostMapping("/searchAssetsType")
    @ApiOperation(value = "查询资讯服务类型", notes = "查询资讯服务类型")
    public DataResponse<List<AssetsTypeDto>> searchAssetsType() {
        List<AssetsTypeDto> allType = assetsService.findAllType(QfContextHolder.getContext().getCorporationCode());
        AssetsTypeDto assetsTypeDto = new AssetsTypeDto();
        assetsTypeDto.setCorporationCode(QfContextHolder.getContext().getCorporationCode());
        assetsTypeDto.setAssetsTypeName("全部");
        allType.add(0,assetsTypeDto);
        return DataResponse.ok(allType);
    }


    @PostMapping("/searchAssets")
    @ApiOperation(value = "查询资讯服务列表", notes = "查询资讯服务列表")
    public DataResponse<Page<AssetsDetailDto>> searchAssets(@RequestBody PageDataRequest<AssetsQueryDto> request) {
        request.getParam().setCityCode(QfContextHolder.getContext().getCityCode());
        Page<AssetsDetailDto> result = assetsService.getListForClient(request.getParam(), PageUtil.getPageable(request));
        return DataResponse.ok(result);
    }


    @PostMapping("/getAssets")
    @ApiOperation(value = "查询资讯服务详情", notes = "查询资讯服务详情")
    public DataResponse<AssetsDetailDto> getAssets(@RequestBody DataRequest<IdDto> request) {
        Optional<Assets> assets = assetsService.findById(request.getParam().getId());

        AssetsDetailDto result = assets.map(assetsMapper::toDto).orElseThrow(() -> new BusinessException("资讯服务业务异常"));

        return DataResponse.ok(result);
    }
}
